from http_factory import HttpClientFactory
from exceptions import RequestError, ResponseError
import asyncio
import time

async def main():
    # 创建异步客户端
    client = HttpClientFactory.create(
        async_mode=True,
        base_url='http://localhost:8000',
        timeout=30,
        max_retries=3
    )
    
    start_time = time.time()
    
    try:
        # 并发请求示例
        tasks = [
            client.get('/messages/history'),
            client.post('/messages/', data={'message': 'Hello from async client'}),
            client.post('/messages/state', params={'key': 'test_async', 'value': 'value_async'}),
        ]
        
        # 等待所有请求完成
        results = await asyncio.gather(*tasks)
        
        print(f"异步GET请求结果: {results[0].data}")
        print(f"异步POST请求结果: {results[1].msg}")
        print(f"异步状态设置结果: {results[2].msg}")
        
        # 获取状态
        response = await client.get('/messages/state/test_async')
        print(f"异步状态获取结果: {response.data}")
        
    except RequestError as e:
        print(f"请求错误: {str(e)}")
    except ResponseError as e:
        print(f"响应错误: {str(e)}")
        
    print(f"异步请求总耗时: {time.time() - start_time:.2f}秒")

if __name__ == '__main__':
    asyncio.run(main()) 